package com.wmx.thread.atomic;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.*;

/**********************************
 * @author wmx
 * @date 2019/8/19 11:48
 * @description:
 * @version 1.0
 * AtomicInteger  AtomicBoolean AtomicLong
 *
 *AtomicIntegerArray  AtomicLongArray AtomicReferenceArray
 ************************************/
public class AtomicTest {

   final static AtomicInteger atomicInteger = new AtomicInteger();
   private static Set<Integer> s=Collections.synchronizedSet(new HashSet<Integer>());

    private volatile  static int value = 0;

    public static void main(String[] args) throws InterruptedException {


    /*  //==============测试valatile=====================
      Thread t =  new Thread(()->{
            for (int a = 0 ; a < 500;a++){
                value += 1;
                s.add(value);
                System.out.println(value);
            }
        });


      Thread t2 =   new Thread(()->{
            for (int a = 0 ; a < 500;a++){
                value += 1;
                s.add(value);
                System.out.println(value);
            }
        });

      t.start();
      t2.start();
      t.join();
      t2.join();
      System.out.println("set的中个数============"+s.size());
    //==============测试valatile=====================
*/
        Thread t2 =  new Thread(()->{
            for (int a = 0 ; a < 500;a++){
                int b = atomicInteger.getAndIncrement();
                System.out.println(b);
                s.add(b);
            }
        });

        Thread t3 =  new Thread(()->{
            for (int a = 0 ; a < 500;a++){
                int b = atomicInteger.getAndIncrement();
                System.out.println(b);
                s.add(b);
            }
        });
        t2.start();
        t3.start();
        t2.join();
        t3.join();
        System.out.println("set的中个数============"+s.size());





    }
}
